import axios from "axios";
import store from "@/store";
import router from "@/router";


const http = axios.create({
    baseURL: "http://172.16.110.100:8080",
    timeout: 3000,
})

http.interceptors.request.use(config => {
    if (store.state.userStore.token) {
        config.headers["token"] = store.state.userStore.token
    }
    return config
}, err => {
    return err
})

http.interceptors.response.use(respone => {
    if (respone.data.token) {
        store.state.userStore.token = respone.data.token
    }
    return respone;
}, err => {
    errList[err.response.status] && errList[err.response.status]()
    return err
})

const errList = {
    401: () => router.replace({ path: "/login", query: { toPath: router.currentRoute.fullPath } }),
}

export default http